package com.ysd.modules.app.controller;

import com.ysd.common.utils.R;
import com.ysd.modules.app.entity.ChapterEntity;
import com.ysd.modules.app.entity.OptionsRecordsEntity;
import com.ysd.modules.app.entity.PracticeResult;
import com.ysd.modules.app.entity.TopicEntity;
import com.ysd.modules.app.service.ChaptersService;
import com.ysd.modules.app.service.OptionsRecordsService;
import com.ysd.modules.app.service.TopicsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author ky
 * @Date 2021/07/13
 * 章节和题目和选项
 */
@RestController
@RequestMapping("/app")
@Api(description = "APP章节和题目，选项相关接口")
public class AppChapterController {
    @Autowired
    private ChaptersService chaptersService;
    @Autowired
    private TopicsService topicsService;
    @Autowired
    private OptionsRecordsService optionsRecordsService;

    @GetMapping("getChapterList")
    @ApiOperation("根据课程id和学生id查询所有章节")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ChapterEntity.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", value = "课程ID", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "stuId", value = "学生id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "类型(1:paper 2:章节练习 3：每日一练)", paramType = "query", dataType = "int")
    })
    public R getChapterList(Integer courseId, Integer stuId, Integer type) {
        List<ChapterEntity> chapterEntityList = chaptersService.getChapterList(courseId, stuId, type);
        return R.ok().put("chapterEntityList", chapterEntityList);
    }

    @PostMapping("getTopicList")
    @ApiOperation("根据章节id获取章节下的题目")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = TopicEntity.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "章节id", paramType = "query", dataType = "int"),
    })
    public R getTopicList(Integer cid) {
        List<TopicEntity> topicEntityList = topicsService.getTopicList(cid);
        return R.ok().put("topicEntityList", topicEntityList);
    }

    @PostMapping("save")
    @ApiOperation("选项提交(需要注意type字段1:试卷练习 2:章节练习(如果是章节练习，paper_id为章节id) 3:每日一练(paper_id=试卷ID))")
    public R save(OptionsRecordsEntity optionsRecordsEntity) {
        boolean result = optionsRecordsService.save(optionsRecordsEntity);
        return R.ok().put("result", result);
    }

    @PostMapping("getPracticeResult")
    @ApiOperation("试卷/章节练习/每日一练-测试结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "targetId", value = "目标id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "stuId", value = "学生id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "类型", paramType = "query", dataType = "int")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = PracticeResult.class)
    })
    public R getPracticeResult(Integer targetId, Integer stuId, Integer type) {
        PracticeResult practiceResult = optionsRecordsService.getPracticeResult(targetId, stuId, type);
        return R.ok().put("practiceResultVO", practiceResult);
    }

    @PostMapping("resetOptionsRecords")
    @ApiOperation("测试结果-重新练习")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tid", value = "章节id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "stuId", value = "学生id", paramType = "query", dataType = "int")
    })
    public R resetOptionsRecords(Integer tid, Integer stuId) {
        Boolean result = optionsRecordsService.resetOptionsRecords(tid, stuId);
        return R.ok().put("result", result);
    }

    @GetMapping("getYesterdayNum")
    @ApiOperation("题库-记录报告-昨天刷题数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stuId", value = "学生id", paramType = "query", dataType = "int")
    })
    public R getYesterdayNum(Integer stuId) {
        int len = optionsRecordsService.getYesterdayNum(stuId);
        return R.ok().put("len", len);
    }

    @PostMapping("resetRecords")
    @ApiOperation("做题记录-一键清空")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stuId", value = "学生id", paramType = "query", dataType = "int")
    })
    public R resetRecords(Integer stuId) {
        int len = optionsRecordsService.resetOptionsRecords(stuId);
        return R.ok().put("len", len);
    }

    @PostMapping("getPracticeResultList")
    @ApiOperation("做题记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stuId", value = "学生id", paramType = "query", dataType = "int")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = PracticeResult.class)
    })
    public R getPracticeResultList(Integer stuId) {
        List<PracticeResult> practiceResults = optionsRecordsService.getPracticeResultList(stuId);
        return R.ok().put("practiceResults", practiceResults);
    }

    @PostMapping("getWrongTopic")
    @ApiOperation("错题集(主要针对章节练习)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stuId", value = "学生ID", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "courseId", value = "课程ID", paramType = "query", dataType = "int")
    })
    public R getWrongTopic(Integer stuId, Integer courseId) {
        Map<String, Object> map = optionsRecordsService.getWrongTopic(stuId, courseId);
        return R.ok().put("map", map);
    }

    @PostMapping("getWrongTopicList")
    @ApiOperation("错题集-查看解析")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = TopicEntity.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stuId", value = "学生id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "cid", value = "章节id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "1:试卷练习 2:章节练习(如果是章节练习，paper_id为章节id) 3: 每日一练(paper_id和试卷练习ID一致)", paramType = "query", dataType = "int"),
    })
    public R getWrongTopicList(Integer stuId, Integer cid, Integer type) {
        List<TopicEntity> topicEntityList = topicsService.getWrongTopicList(stuId, cid, type);
        return R.ok().put("topicEntityList", topicEntityList);
    }

    @PostMapping("renewalWrongTopicList")
    @ApiOperation("错题集-重做")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = TopicEntity.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stuId", value = "学生id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "cid", value = "章节id", paramType = "query", dataType = "int"),
    })
    public R renewalWrongTopicList(Integer stuId, Integer cid) {
        List<TopicEntity> topicEntityList = topicsService.renewalWrongTopicList(stuId, cid);
        return R.ok().put("topicEntityList", topicEntityList);
    }

}
